package com.yunc.modules.quartz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunc.base.aspect.Log;
import com.yunc.base.schedule.SchedulerUtils;
import com.yunc.base.utils.R;
import com.yunc.base.utils.ScheduleConstant;
import com.yunc.modules.quartz.entity.SysJob;
import com.yunc.modules.quartz.service.ISysJobService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 定时任务
 *
 * @author congcong tao
 * @date 2018/11/12 10:21
 */
@RestController
@RequestMapping("sysJob")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysJobController {

    private final Scheduler scheduler;
    private final ISysJobService sysJobService;
    
    @GetMapping("/page")
    public R page(Page<SysJob> page, SysJob sysJob){
        QueryWrapper<SysJob> qw = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(sysJob.getJobName())) {
            qw.eq("job_name", sysJob.getJobName());
        }
        sysJobService.page(page, qw);
        return R.ok().put("data", page.getRecords()).put("count", page.getTotal());
    }

    /**
     * 定时任务信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable String id){
        SysJob job = sysJobService.getById(id);
        return R.ok().put("sysJob", job);
    }

    /**
     * 保存
     */
    @Log("定时任务")
    @PostMapping("/save")
    @PreAuthorize("@ss.hasPermi('sys:job:save')")
    public R save(SysJob job) throws SchedulerException {
        boolean flag = sysJobService.save(job);
        if (flag) {
            SchedulerUtils.createScheduleJob(scheduler, job);
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @Log("定时任务")
    @PutMapping(value = "/update")
    @PreAuthorize("@ss.hasPermi('sys:job:update')")
    public R update(SysJob job) throws SchedulerException {
        boolean flag = sysJobService.updateById(job);
        if (flag) {
            SchedulerUtils.updateScheduleJob(scheduler, job);
            return R.ok();
        }
        return R.error();
    }

    @Log("定时任务")
    @PutMapping(value = "/updateCronExpression")
    @PreAuthorize("@ss.hasPermi('sys:job:update')")
    public R updateCronExpression(Long id, String cronExpression) throws SchedulerException {
        SysJob sysJob = sysJobService.getById(id);
        sysJob.setCronExpression(cronExpression);
        boolean flag = sysJobService.updateById(sysJob);
        if (flag) {
            SchedulerUtils.updateScheduleJob(scheduler, sysJob);
            return R.ok();
        }
        return R.error();
    }

    @Log("定时任务")
    @PutMapping(value = "/resume/{id}")
    @PreAuthorize("@ss.hasPermi('sys:job:resumeJob')")
    public R resumeJob(@PathVariable Long id) throws SchedulerException {
        SysJob job = sysJobService.getById(id);
        job.setStatus(ScheduleConstant.Status.NORMAL.getValue());
        boolean flag = sysJobService.updateById(job);
        if (flag) {
            SchedulerUtils.resumeJob(scheduler, id);
            return R.ok();
        }
        return R.error();
    }

    @Log("定时任务")
    @PutMapping(value = "/pause/{id}")
    @PreAuthorize("@ss.hasPermi('sys:job:pauseJob')")
    public R pauseJob(@PathVariable Long id) throws SchedulerException {
        SysJob job = sysJobService.getById(id);
        job.setStatus(ScheduleConstant.Status.PAUSE.getValue());
        boolean flag = sysJobService.updateById(job);
        if (flag) {
            SchedulerUtils.pauseJob(scheduler, id);
            return R.ok();
        }
        return R.error();
    }

    @PutMapping(value = "/run/{id}")
    @PreAuthorize("@ss.hasPermi('sys:job:runJob')")
    public R run(@PathVariable Long id) throws SchedulerException {
        SysJob jobRecord = sysJobService.getById(id);
        SchedulerUtils.run(scheduler, jobRecord);
        return R.ok();
    }
}
